﻿

namespace QdArch
{
    public class StairAction : ComponentInstance2dAction
    {
        private PointInputer PointInputer { get; set; }

        public StairAction() { }
        public StairAction(IDocumentEditor docEditor) : base(docEditor) 
        {
            this.commandCtrl.WriteInfo("命令：Stair");
        }
        public async void ExecCreate(string[] args = null)
        {
            //1. 选择插入点
            this.PointInputer = new PointInputer(this.docEditor);
            QdStairInstance stairInstance = null;
            var result = await this.PointInputer.Execute("插入点:");
            if (result == null || result.ValueX == null || !(result.ValueX is Vector2)||this.PointInputer.isCancelled)
            {
                this.Cancel();
                goto End;
            }
            var stairName = "DogleggedStair";
            //var stair = docRt.Document.Components.Find(ArchCategory.Stair.ToString(), stairName) as QdStair;
            //if (stair == null)
            //{
            //    stair = new QdStair(StairCategory.DogleggedStair.ToString(), stairName);
            //    docRt.Document.Components.Add(stair);
            //}
            //var stair = ComponentManager.GetCptDefs("楼梯", "双跑楼梯").FirstOrDefault();
            var stair = new QdStair();
            var instP = (Vector2)result.ValueX;
            stairInstance = new QdStairInstance(ComponentManager.GetCptDef<QdStair>("楼梯", "双跑楼梯", StairAttribute.BuiltinUuid));
            //stairInstance.Matrix = Matrix3.GetTranslate(instP);
            //stairInstance.Parameters = QdStair.ParamsDefinition.New();
            stairInstance.StepHeight = 150;
            stairInstance.StepWidth = 260;
            stairInstance.StepNum = 10;
            stairInstance.LadderWidth = 1000;
            stairInstance.PlatLen = 2100;
            stairInstance.PlatWidth = 1200;
            stairInstance.PlatThickness = 120;
            stairInstance.SlabThickness = 100;
            stairInstance.Position.Set(instP.X,instP.Y,0);
            this.DrawStair(stairInstance);

        //stairInstance.stair = stair;
        End:
            this.PointInputer = null;
            this.EndCreating();
        }
        public override void Draw(SKCanvas canvas, LcElement element, Vector2 offset)
        {
            var stairInstance = element as QdStairInstance;
            var curves = stairInstance.Curves?.FirstOrDefault().Curve2ds;
            var pen = this.GetDrawPen(element);
            if (pen == LightCAD.Runtime.Constants.defaultPen)
            {
                pen = new SKPaint { Color = SKColors.White, IsStroke = true };
            }
            DrawStairCurves(canvas, curves, /*stairInstance.Matrix,*/ pen);
        }
        public override void Draw(SKCanvas canvas, LcElement element, Matrix3 matrix)
        {
            var stairInstance = element as QdStairInstance;
            // var curves = stairInstance.ShapeCurves;
            var curves = stairInstance.Curves?.FirstOrDefault().Curve2ds;
            var pen = this.GetDrawPen(element);
            if (pen == LightCAD.Runtime.Constants.defaultPen)
            {
                pen = new SKPaint { Color = SKColors.White, IsStroke = true };
            }
            //matrix = matrix * stairInstance.Matrix;
            DrawStairCurves(canvas, curves, /*matrix, */pen);
        }
        //public void DrawStairCurves(SKCanvas canvas, List<Curve2d> curves, Matrix3 matrix, SKPaint pen)
        public void DrawStairCurves(SKCanvas canvas, List<Curve2d> curves,  SKPaint pen)
        {
            foreach (var curve in curves)
            {
                switch (curve.Type)
                {
                    case Curve2dType.Rect2d:
                        {
                            var rect = curve as Rect2d;
                            var min = rect.Min;
                            var max = rect.Max;
                            //var a = this.vportRt.ConvertWcsToScr(matrix.MultiplyPoint(min)).ToSKPoint();
                            //var b = this.vportRt.ConvertWcsToScr(matrix.MultiplyPoint(new Vector2(max.X,min.Y))).ToSKPoint();
                            //var c = this.vportRt.ConvertWcsToScr(matrix.MultiplyPoint(max)).ToSKPoint();
                            //var d = this.vportRt.ConvertWcsToScr(matrix.MultiplyPoint(new Vector2(min.X,max.Y))).ToSKPoint();
                            var a = this.vportRt.ConvertWcsToScr(min).ToSKPoint();
                            var b = this.vportRt.ConvertWcsToScr(new Vector2(max.X, min.Y)).ToSKPoint();
                            var c = this.vportRt.ConvertWcsToScr(max).ToSKPoint();
                            var d = this.vportRt.ConvertWcsToScr(new Vector2(min.X, max.Y)).ToSKPoint();
                            canvas.DrawPoints(SKPointMode.Polygon,new SKPoint[] { a, b, c, d,a }, pen);
                            break;
                        }
                    default:
                        break;
                }
            }
        }

        private void DrawStair(QdStairInstance stairInstance) 
        {
            if (stairInstance == null)
                return;
            stairInstance.Initilize(this.docRt.Document);
            this.docRt.Document.ModelSpace.InsertElement(stairInstance);
        }
        public override void Cancel()
        {
            base.Cancel();
            this.vportRt.SetCreateDrawer(null);
        }

        #region Grip
        private QdStairInstance _stairInstance;
        private string _gripName;
        private Vector2 _position;

        public override ControlGrip[] GetControlGrips(LcElement element)
        {
            var stairInstance = element as QdStairInstance;
            var grips = new List<ControlGrip>();

            var gripInsert = new ControlGrip
            {
                Element = stairInstance,
                Name = "InsertPoint",
                Position = stairInstance.Position.ToVector2()
            };
            grips.Add(gripInsert);
            return grips.ToArray();
        }

        public override void SetDragGrip(LcElement element, string gripName, Vector2 position, bool isEnd)
        {
            var stairInstance = element as QdStairInstance;
            _stairInstance = stairInstance;
            if (!isEnd)
            {
                _gripName = gripName;
                _position = position;
            }
            else
            {
                if (gripName == "InsertPoint")
                {
                    var offset = position.Sub( stairInstance.Position.ToVector2());
                    stairInstance.Translate(offset);
                    //stairInstance.Matrix = Matrix3.GetTranslate(offset)*stairInstance.Matrix ;
                }
            }

        }

        public override void DrawDragGrip(SKCanvas canvas)
        {
            if (_stairInstance == null)
                return;
            if (_gripName == "InsertPoint")
            {
                var offset = _position - _stairInstance.Position.ToVector2();
              //  var matrix = Matrix3.GetTranslate(offset) * _stairInstance.Matrix;
                var curves = _stairInstance.Curves;
                DrawStairCurves(canvas, curves?.FirstOrDefault().Curve2ds,/* matrix,*/ LightCAD.Runtime.Constants.draggingPen);
            }

        }
        #endregion

    }
    public class Stair3dAction : ComponentInstance3dAction
    {
        //public override List<Object3D> Render(IComponentInstance cptIns)
        //{
        //    var stairInstance = cptIns as QdStairInstance;
        //    var qdStair = stairInstance.Definition as QdStair;

        //}
        public Stair3dAction() : base()
        { }
        public Stair3dAction(IDocumentEditor docEditor) : base(docEditor)
        {
            this.commandCtrl.WriteInfo("命令：Stair");
        }
        public async void ExecCreate(string[] args = null)
        {
            QdStairInstance stairInstance = new QdStairInstance(ComponentManager.GetCptDef<QdStair>("楼梯", "双跑楼梯", StairAttribute.BuiltinUuid));
            //stairInstance.Matrix = Matrix3.GetTranslate(instP);
            //stairInstance.Parameters = QdStair.ParamsDefinition.New();
            stairInstance.StepHeight = 150;
            stairInstance.StepWidth = 260;
            stairInstance.StepNum = 10;
            stairInstance.LadderWidth = 1000;
            stairInstance.PlatLen = 2100;
            stairInstance.PlatWidth = 1200;
            stairInstance.PlatThickness = 120;
            stairInstance.SlabThickness = 100;
            stairInstance.Position.Set(0, 0, 0);
            stairInstance.Initilize(this.docRt.Document);
            stairInstance.Reset();
             (stairInstance.Rt3DAction as IComponentAction).SetDocEditor(this.docEditor);
            this.docRt.Document.ModelSpace.InsertElement(stairInstance);
        }
    }
}
